package com.meixiang.view.photoSelectActivity.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.meixiang.R;
import com.meixiang.view.photoSelectActivity.util.BitmapCache;
import com.meixiang.view.photoSelectActivity.util.ImageItem;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/13 15:26
 * <p/>
 * 相册适配器
 */
public class PhotoAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    final String TAG = getClass().getSimpleName();


    private ArrayList<ImageItem> dataList;
    private ArrayList<ImageItem> selectedDataList;

    private Context context;

    BitmapCache cache;

    private Intent mIntent;

    public PhotoAlbumAdapter(Context context, ArrayList<ImageItem> dataList, ArrayList<ImageItem> selectedDataList) {
        cache = new BitmapCache();
        this.context = context;
        this.dataList = dataList;
        this.selectedDataList = selectedDataList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_photo_album, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
//                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                    // 点击item进入详情界面


                }
            });

            ImageItem bean = dataList.get(position);
            final ImageItem item = dataList.get(position);

            ((ViewHolder) holder).mImageView.setTag(item.imagePath);


            cache.displayBmp(((ViewHolder) holder).mImageView, item.thumbnailPath, item.imagePath,
                    callback);
            if (bean != null) {

//                ((ViewHolder) holder).mToggleButton.setTag(position);
                ((ViewHolder) holder).mCbIntegral.setTag(position);
//                ((ViewHolder) holder).mToggleButton.setOnClickListener(new ToggleClickListener(((ViewHolder) holder).mCbIntegral));
//                ((ViewHolder) holder).mCbIntegral.setOnClickListener(new ToggleClickListener(((ViewHolder) holder).mCbIntegral));
                ((ViewHolder) holder).mCbIntegral.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = (Integer)  ((ViewHolder) holder).mCbIntegral.getTag();
                        if (dataList != null && mOnItemClickListener != null
                                && position < dataList.size()) {
//                            mOnItemClickListener.onItemClick(mCbIntegral, position, );
                        }
                    }
                });

            }


        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, boolean isChecked);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //当前显示的图片
        @Bind(R.id.image_view)
        ImageView mImageView;
        @Bind(R.id.cb_integral)
        CheckBox mCbIntegral;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }


    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                    Log.e(TAG, "callback, bmp not match");
                }
            } else {
                Log.e(TAG, "callback, bmp null");
            }
        }
    };


//    private class ToggleClickListener implements View.OnClickListener {
//        CheckBox chooseBt;
//
//        public ToggleClickListener(CheckBox choosebt) {
//            this.chooseBt = choosebt;
//        }
//
//        @Override
//        public void onClick(View view) {
//            if (view instanceof ToggleButton) {
//                ToggleButton toggleButton = (ToggleButton) view;
//                int position = (Integer) toggleButton.getTag();
//                if (dataList != null && mOnItemClickListener != null
//                        && position < dataList.size()) {
//                    mOnItemClickListener.onItemClick(toggleButton, position, toggleButton.isChecked(), chooseBt);
//                }
//            }
//        }
//    }


}
